﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DoAn.BUS;
using DoAn.DTO;
using System.Data;


namespace DoAn
{
    public partial class ChiTietSanPham : System.Web.UI.Page
    {
        
        //public SanDauGiaDTO san = new SanDauGiaDTO();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

                loadall();

            }
            
            

        }
        void loadall()
        {
            if (Request.QueryString["masan"] == null)
                Response.Redirect("~/TrangChu.aspx");
            int msan = Convert.ToInt32(Request.QueryString["masan"].ToString());
            DataTable dt = SanDauGiaBUS.laysptheoma(msan);
            loadhinh(dt);
            SanDauGiaDTO san = (SanDauGiaDTO)Session["san"];
            if (kiemtratinhtrangsan(dt) == true)
                ASPxRoundPanel2.Visible = false;
            else
            {
                lbtensanpham.Text = san.TenSan;
                lbgiahientai.Text = (san.GiaSanHienTai).ToString("###,###,###,### vnd").Replace(',', '.');
                if (san.MuaNgay == true)
                    lbmuangay.Text = (san.GiaMuaNgay).ToString("###,###,###,### vnd").Replace(',', '.');
                else
                {
                    lbmuangay.Text = 0 + "vnd";
                    Button2.Visible = false;

                }
               

            }
            // thong tin ngan gon
            lbgiahientai.Text = (san.GiaSanHienTai).ToString("###,###,###,### vnd").Replace(',', '.');
            lbthoigianketthuc.Text = san.NgayKetThucDang.ToString();
            lbthoigianconlai.Text = (Convert.ToDateTime(san.NgayKetThucDang) - DateTime.Now).ToString();
            //lbthoigianconlai.Text = time.Hour.ToString() + " " + time.Second.ToString() + " " + time.Millisecond.ToString() + " " + time.Day.ToString() + "  " + time.Month.ToString() + " " + time.Year.ToString();
            lbdiachi.Text = san.DiaChiShip;
            lbnguoiban.Text = san.TenTaiKhoan;
            lbgiahienthoi.Text = san.GiaSanHienTai.ToString("###,###,###,### vnd").Replace(',', '.');
            //thong tin san pham
            lbthongtinchung.Text = san.MoTaChiTiet;
            lbthongtinchitiet.Text = "www.google.com.vn/Không tìm thấy";
            loadquatrinhdaugia(san.MaSan);
            loadchiphi(san.MaSan);
            bool kt = Convert.ToBoolean(san.HoTroTraSP);
            if (kt == true)
            {
                lbchopheptra.Text = "Có";
                lbthoihan.Text = san.ThoiGianTraSP.ToString();
            }

            else
            {
                lbchopheptra.Text = "Không";
                lbthoihan.Text = "0";
            }
        }
        void loadchiphi(int ma)
        {
            DataTable dt = ChiPhiVanChuyenBUS.layds3bang(ma);
            foreach (DataRow i in dt.Rows)
            {
                if (float.Parse(i["MaPhiVung"].ToString()) == 1)
                {
                    Session["chuyenpn"] = i["CuocPhi"].ToString();
                }
                else
                    Session["chuyenthuong"] = i["CuocPhi"].ToString();
            }
            GridView2.DataSource = dt;
            GridView2.DataBind();
            DataTable dt2 = ChiTietThanhToanBUS.loadchitiettheoma(ma);
            for (int i = 0; i < dt2.Rows.Count; i++)
            {
                string ten = dt2.Rows[i]["TenHinhThucTT"].ToString();
               
                lbhinhthucthanhtoan.Text += (i+1).ToString()+ "-  "+ ten + "<br />";
            }

        }
        void loadquatrinhdaugia(int ma)
        {
            DataTable dt = QuaTrinhDauGiaBUS.laydstheoma(ma);
            GridView1.DataSource = dt;
            GridView1.DataBind();
            if (dt.Rows.Count > 0)
            {
                lbquatrinhdau.Text = dt.Rows.Count.ToString();
                lbnguoiragiacaonhat.Text = (Int32.Parse(dt.Rows[0]["SoTien"].ToString())).ToString("###,###,###,### vnd").Replace(',', '.');
            }
            else
            {
                lbquatrinhdau.Text = "Chưa có";
                lbnguoiragiacaonhat.Text = "Chưa có";
            }
            
            
        
        }
        bool kiemtratinhtrangsan(DataTable dt)
        {
            foreach (DataRow row in dt.Rows)
            {
                bool dong =Convert.ToBoolean( row["KetThucSan"].ToString());
                if (dong == true)
                    return true;
            }
            return false;
        }
        private void loadhinh(DataTable dt)
        {
            SanDauGiaDTO san = new SanDauGiaDTO();
            string dong ="";
            foreach (DataRow row in dt.Rows)
            {
                san.MaSan = Int32.Parse(row["MaSan"].ToString());
                san.TenSan =(row["TenSan"].ToString());
                san.MoTaChiTiet = row["MoTaChiTiet"].ToString();
                san.GiaSanHienTai = float.Parse(row["GiaSanHienTai"].ToString());
                san.NgayKetThucDang = Convert.ToDateTime(row["NgayKetThucDang"].ToString());
                san.NgayDangSP = Convert.ToDateTime(row["NgayDangSP"].ToString());
                san.DiaChiShip = (row["DiaChiShip"].ToString());
                san.TenTaiKhoan = row["TenTaiKhoan"].ToString();//---------------------------------------tendang nhap nho sua
                san.HoTroTraSP = Convert.ToBoolean(row["HoTroTraSP"].ToString());
                san.ThoiGianTraSP = Convert.ToInt32(row["ThoiGianTraSP"].ToString());
                san.GiaMuaNgay = Convert.ToInt32(row["GiaMuaNgay"].ToString());
                san.MuaNgay = Convert.ToBoolean(row["MuaNgay"].ToString());
                 dong= row["HinhAnh"].ToString();
            }
            string[] dongarr = dong.Split(new char[] { ';' });
            DataTable dttemp = new DataTable();
            dttemp.Columns.Add("HinhAnh", typeof(string));
            for (int i = 0; i < dongarr.Length-1; i++)
            {
                DataRow row =  dttemp.NewRow();
                
                dongarr[i] = "~/BAN/HinhAnh/" + dongarr[i];
                row[0] = dongarr[i];
                dttemp.Rows.Add(row);
                //Response.Write(row[0].ToString());
            }
            Image1.ImageUrl = dongarr[0];
            DataList1.DataSource = dttemp;
            DataList1.DataBind();
            Session["san"] = san;
        }

        protected void ImageButton1_Command(object sender, CommandEventArgs e)
        {
            Image1.ImageUrl = e.CommandArgument.ToString();
        }
        void insert()
        {
            SanDauGiaDTO san = (SanDauGiaDTO)Session["san"];
            QuaTrinhDauGiaDTO q = new QuaTrinhDauGiaDTO();
            q.MaSan = san.MaSan;
            q.SoTien = float.Parse(txtragia.Text);
            q.TenTaiKhoan = Session["tendangnhap"].ToString();
            
            QuaTrinhDauGiaBUS.insert_ragia(q);
            SanDauGiaBUS.update_giahientai(san.MaSan, q.SoTien);
            loadall();
            Response.Redirect("~/ChiTietSanPham.aspx?masan="+san.MaSan+"");
        }


        //Session["tendangnhap"] = tk.TenTaiKhoan;
        //    Session["mathe"] = tk.MaThe;
        protected void Button1_Click1(object sender, EventArgs e)
        {
            HttpCookie tmpCookie = System.Web.HttpContext.Current.Request.Cookies["TenTaiKhoan"];
            if (Session["tendangnhap"] == null && tmpCookie == null)
            {

                System.Windows.Forms.MessageBox.Show("Bạn chưa đăng nhập", "Thông báo");

                Response.Redirect("~/DangNhap.aspx");
            }
            insert();
            
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            HttpCookie tmpCookie = System.Web.HttpContext.Current.Request.Cookies["TenTaiKhoan"];
            if (Session["tendangnhap"] == null && tmpCookie == null)
            {

                System.Windows.Forms.MessageBox.Show("Bạn chưa đăng nhập", "Thông báo");
                //System.Threading.Thread.Sleep(3000);
                Response.Redirect("~/DangNhap.aspx");
            }
            SanDauGiaDTO san = (SanDauGiaDTO)Session["san"];
            QuaTrinhDauGiaDTO q = new QuaTrinhDauGiaDTO();
            q.MaSan = san.MaSan;
            q.SoTien = san.GiaMuaNgay;
            q.TenTaiKhoan = san.TenTaiKhoan;
            Session["quatrinh"] = q;
            Response.Redirect("~/ThanhToan.aspx");
            QuaTrinhDauGiaBUS.insert_ragia(q);
            SanDauGiaBUS.update_muangay(san.MaSan,san.GiaMuaNgay);
            Response.Redirect("~/ChiTietSanPham.aspx?masan=" + san.MaSan + "");
            

        }
    }
}